home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / util / gnu / xpdf-0.8-src.lha / xpdf-0.8-src / ltk / LTKLabel.cc < prev    next >
C/C++ Source or Header  |  1998-11-28  |  3KB  |  110 lines

  1. //========================================================================
  2. //
  3. // LTKLabel.cc
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8.  
  9. #ifdef __GNUC__
  10. #pragma implementation
  11. #endif
  12.  
  13. #include <stdlib.h>
  14. #include <stdarg.h>
  15. #include <stddef.h>
  16. #include <X11/Xlib.h>
  17. #include <X11/Xutil.h>
  18. #include "LTKLabel.h"
  19.  
  20. LTKLabel::LTKLabel(char *name1, int widgetNum1,
  21.            LTKLabelSize size1, int maxLength1,
  22.            char *fontName1, char *text1):
  23.     LTKWidget(name1, widgetNum1) {
  24.   size = size1;
  25.   if (size == ltkLabelMaxLength)
  26.     maxLength = maxLength1;
  27.   else
  28.     maxLength = -1;
  29.   text = text1 ? new GString(text1) : new GString();
  30.   length = text->getLength();
  31.   if (maxLength >= 0 && length > maxLength)
  32.     length = maxLength;
  33.   fontName = fontName1;
  34.   fontStruct = NULL;
  35.   textGC = None;
  36. }
  37.  
  38. LTKLabel::~LTKLabel() {
  39.   delete text;
  40.   if (fontName && fontStruct) {
  41.     XFreeFont(getDisplay(), fontStruct);
  42.     XFreeGC(getDisplay(), textGC);
  43.   }
  44. }
  45.  
  46. void LTKLabel::setText(char *text1) {
  47.   if (size == ltkLabelStatic)
  48.     return;
  49.   delete text;
  50.   text = text1 ? new GString(text1) : new GString();
  51.   length = text->getLength();
  52.   if (maxLength >= 0 && length > maxLength)
  53.     length = maxLength;
  54.   if (getXWindow() != None) {
  55.     clear();
  56.     redraw();
  57.   }
  58. }
  59.  
  60. void LTKLabel::layout1() {
  61.   XCharStruct extents;
  62.   int direction, ascent, descent;
  63.   XGCValues gcValues;
  64.   int textWidth;
  65.  
  66.   if (textGC == None) {
  67.     if (fontName &&
  68.     (fontStruct = XLoadQueryFont(getDisplay(), fontName))) {
  69.       XGetGCValues(getDisplay(), getFgGC(),
  70.            GCForeground | GCBackground | GCGraphicsExposures,
  71.            &gcValues);
  72.       gcValues.font = fontStruct->fid;
  73.       textGC = XCreateGC(getDisplay(), parent->getXWindow(),
  74.              GCForeground | GCBackground | GCGraphicsExposures |
  75.              GCFont, &gcValues);
  76.     } else {
  77.       fontName = NULL;
  78.       fontStruct = parent->getXFontStruct();
  79.       textGC = getFgGC();
  80.     }
  81.   }
  82.   textWidth = 0;
  83.   switch (size) {
  84.   case ltkLabelStatic:
  85.     XTextExtents(fontStruct, text->getCString(), text->getLength(),
  86.          &direction, &ascent, &descent, &extents);
  87.     textWidth = extents.width;
  88.     break;
  89.   case ltkLabelFixedWidth:
  90.     textWidth = 0;
  91.     break;
  92.   case ltkLabelMaxLength:
  93.     textWidth = maxLength * fontStruct->max_bounds.width;
  94.     break;
  95.   }
  96.   textHeight = fontStruct->ascent + fontStruct->descent;
  97.   textBase = fontStruct->ascent;
  98.   width = textWidth + 12;
  99.   height = textHeight + 4;
  100. }
  101.  
  102. void LTKLabel::redraw() {
  103.   int tx, ty;
  104.  
  105.   tx = 6;
  106.   ty = (height - textHeight) / 2 + textBase;
  107.   XDrawString(getDisplay(), xwin, textGC, tx, ty,
  108.           text->getCString(), length);
  109. }
  110.